package com.css.dzj.dzjap.modules.expertcredit.service.impl;

import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.common.util.ServiceUtil;
import com.css.dzj.dzjap.common.util.SnowflakeId;
import com.css.dzj.dzjap.modules.expertcredit.dao.ExpertCreditDao;
import com.css.dzj.dzjap.modules.expertcredit.entity.ApExpertCreditEntity;
import com.css.dzj.dzjap.modules.expertcredit.entity.QApExpertCreditEntity;
import com.css.dzj.dzjap.modules.expertcredit.service.ExpertCreditService;
import com.css.dzj.dzjap.modules.info.service.ExpertService;
import com.css.dzj.dzjap.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ExpertCreditServiceImpl implements ExpertCreditService{


    @Autowired
    private ExpertCreditDao expertDao;

    //注入JPAQueryFactory工厂
    @Autowired
    JPAQueryFactory jpaQueryFactory;

    //专家
    @Autowired
    private ExpertService eService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    ServiceUtil serviceUtil;
    /**
     * 通过ID查询专家信用表
     */
    @Override
    public ApExpertCreditEntity getExpertCred(String expertcreditId) {

        if(StringUtils.isEmpty(expertcreditId)){

            throw new RuntimeException("参数为空");
        }
        QApExpertCreditEntity qExpert = QApExpertCreditEntity.apExpertCreditEntity;
        return jpaQueryFactory
                .selectFrom(qExpert)
                .where(  qExpert.expertcreditId.eq(expertcreditId),
                         qExpert.delFlag.eq(Constant.NO_DELETE),
                         qExpert.excpresState.eq(Constant.EXCPRES_STATE_NO)
                )
                .fetchOne();
    }


    /**
     *添加专家信用表
     * @return
     */
    @Override
    @Transactional
    public String addExpertCred(ApExpertCreditEntity exCred) {
        QApExpertCreditEntity qExpert = QApExpertCreditEntity.apExpertCreditEntity;

        ApExpertCreditEntity cred = jpaQueryFactory
                                      .selectFrom(qExpert)
                                      .where(qExpert.delFlag.eq(Constant.NO_DELETE),
                                              qExpert.expertCode.eq(exCred.getExpertCode()).or(qExpert.expertIdcard.eq(exCred.getExpertIdcard()))

                                     )
                                      .fetchOne();
        if (cred!=null){
            throw new RuntimeException("专家评分编号或专家证件号码已存在！");
        }
        R r = serviceUtil.getUserInfoByRequest(request);
        if(r.getCode().equals("200")){
            LoginUser userInfo =  (LoginUser)r.get("data");
            exCred.setCreateUser(userInfo.getLoginName());
            exCred.setOrganName(userInfo.getOrgName());
        }
        eService.updateBlack("0",exCred.getExpertIdcard());
        exCred.setExpertNotice("0");
        exCred.setDelFlag(Constant.NO_DELETE);
        exCred.setExcpresState(Constant.EXCPRES_STATE_NO);
        exCred.setExpertcreditId(SnowflakeId.getId());
        exCred.setCreateTime(new Date());
        try {
            expertDao.save(exCred);
            return exCred.getExpertcreditId();

        }catch (Exception e){
            return "0";
        }
    }

    /**
     * 修改专家信用信息表
     */
    @Override
    @Transactional
    public void update(ApExpertCreditEntity exCred) {

    }

    //删除专家信用信息表
    @Override
    @Transactional
    public Boolean deleteExpertCred(String expertcreditId) {
        QApExpertCreditEntity qExpert = QApExpertCreditEntity.apExpertCreditEntity;
        if(StringUtils.isEmpty(expertcreditId)){
            return  false;
        }

        try{
            jpaQueryFactory
                    .update(qExpert)
                    .set(qExpert.delFlag,Constant.DELETE)
                    .where(
                            qExpert.delFlag.eq(Constant.NO_DELETE),
                            qExpert.excpresState.eq(Constant.EXCPRES_STATE_NO),
                            qExpert.expertcreditId.eq(expertcreditId)
                    )
                    .execute();
            return true;
        }catch (Exception e){
            return false;
        }
    }



    /**
     * 查询专家信用信息表黑名单
     */
    @Override
    public ApExpertCreditEntity getReExpertCred(String expertcreditId) {
        if(StringUtils.isEmpty(expertcreditId) ){

            throw new RuntimeException("参数为空");
        }
        QApExpertCreditEntity qExpert = QApExpertCreditEntity.apExpertCreditEntity;
        return jpaQueryFactory
                .selectFrom(qExpert)
                .where(  qExpert.expertcreditId.eq(expertcreditId),
                        qExpert.delFlag.eq(Constant.NO_DELETE),
                        qExpert.excpresState.eq(Constant.EXCPRES_STATE_Yes)
                )
                .fetchOne();
    }

    /**
     *分页查询所有专家信用信息表
     */
    @Override
    public QueryResults<ApExpertCreditEntity> getAllExpertCred(Pageable pageable) {

        QApExpertCreditEntity qExpert = QApExpertCreditEntity.apExpertCreditEntity;
        Predicate predicate = qExpert.isNotNull().or(qExpert.isNull());
        predicate = ExpressionUtils.and(predicate, qExpert.delFlag.eq(Constant.NO_DELETE)) ;
        predicate = ExpressionUtils.and(predicate,qExpert.excpresState.eq(Constant.EXCPRES_STATE_NO));

        return  jpaQueryFactory
                .selectFrom(qExpert)
                .where(
                        predicate
                )
                .orderBy(qExpert.createTime.desc())
                .offset(pageable.getOffset())   //起始页
                .limit(pageable.getPageSize())  //每页大小
                .fetchResults();
    }

    /**
     *分页查询所有专家信用信息表黑名单
     */
    @Override
    public QueryResults<ApExpertCreditEntity> getAllReExpertCred(Pageable pageable) {
        QApExpertCreditEntity qExpert = QApExpertCreditEntity.apExpertCreditEntity;
        Predicate predicate = qExpert.isNotNull().or(qExpert.isNull());
        predicate = ExpressionUtils.and(predicate, qExpert.delFlag.eq(Constant.NO_DELETE)) ;
        predicate = ExpressionUtils.and(predicate,qExpert.excpresState.eq(Constant.EXCPRES_STATE_Yes));

        return  jpaQueryFactory
                .selectFrom(qExpert)
                .where(
                        predicate
                )
                .orderBy(qExpert.createTime.desc())
                .offset(pageable.getOffset())   //起始页
                .limit(pageable.getPageSize())  //每页大小
                .fetchResults();
    }

    /**
     *多条件分页查询所有专家信用信息表
     */
    @Override
    public QueryResults<ApExpertCreditEntity> getAllConExpertCred(Pageable pageable, String expertName,String expertIdcard,String certype,String expertCode,String expertScore,String expertGrade,String expertClassification) {
        QApExpertCreditEntity qExpert = QApExpertCreditEntity.apExpertCreditEntity;
        Predicate predicate = qExpert.isNotNull().or(qExpert.isNull());

        predicate = ("".equals(expertIdcard) || expertIdcard == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertIdcard.like("%"+expertIdcard+"%"));
        predicate = ("".equals(certype) || certype == null) ? predicate : ExpressionUtils.and(predicate, qExpert.certype.eq(certype));
        predicate = ("".equals(expertCode) || expertCode == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertCode.like("%"+expertCode+"%"));
        predicate = ("".equals(expertScore) || expertScore == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertScore.eq(expertScore));
        predicate = ("".equals(expertGrade) || expertGrade == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertLevl.eq(expertGrade));
        predicate = ("".equals(expertClassification) || expertClassification == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertClassification.eq(expertClassification));

        predicate = ("".equals(expertName) || expertName == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertName.like("%"+expertName+"%"));
        predicate = ExpressionUtils.and(predicate, qExpert.delFlag.eq(Constant.NO_DELETE)) ;
        predicate = ExpressionUtils.and(predicate,qExpert.excpresState.eq(Constant.EXCPRES_STATE_NO));

        return  jpaQueryFactory
                .selectFrom(qExpert)
                .where(
                        predicate
                )
                .orderBy(qExpert.createTime.desc())
                .offset(pageable.getOffset())   //起始页
                .limit(pageable.getPageSize())  //每页大小
                .fetchResults();
    }


    /**
     *多条件分页查询所有专家信用信息表黑名单
     */
    @Override
    public QueryResults<ApExpertCreditEntity> getAllReConExpertCred(Pageable pageable, String expertName,String expertIdcard,String certype,String expertCode,String expertScore,String expertLevl,String expertClassification) {
        QApExpertCreditEntity qExpert = QApExpertCreditEntity.apExpertCreditEntity;
        Predicate predicate = qExpert.isNotNull().or(qExpert.isNull());

        predicate = ("".equals(expertIdcard) || expertIdcard == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertIdcard.like("%"+expertIdcard+"%"));
        predicate = ("".equals(certype) || certype == null) ? predicate : ExpressionUtils.and(predicate, qExpert.certype.eq(certype));
        predicate = ("".equals(expertCode) || expertCode == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertCode.like("%"+expertCode+"%"));
        predicate = ("".equals(expertScore) || expertScore == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertScore.eq(expertScore));
        predicate = ("".equals(expertLevl) || expertLevl == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertLevl.eq(expertLevl));
        predicate = ("".equals(expertClassification) || expertClassification == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertClassification.eq(expertClassification));

        predicate = ("".equals(expertName) || expertName == null) ? predicate : ExpressionUtils.and(predicate, qExpert.expertName.like("%"+expertName+"%"));
        predicate = ExpressionUtils.and(predicate, qExpert.delFlag.eq(Constant.NO_DELETE)) ;
        predicate = ExpressionUtils.and(predicate,qExpert.excpresState.eq(Constant.EXCPRES_STATE_Yes));

        return  jpaQueryFactory
                .selectFrom(qExpert)
                .where(
                        predicate
                )
                .orderBy(qExpert.createTime.desc())
                .offset(pageable.getOffset())   //起始页
                .limit(pageable.getPageSize())  //每页大小
                .fetchResults();
    }

    /**
     *批量删除
     */
    @Override
    @Transactional
    public void update(List<ApExpertCreditEntity> list) {

    }

    /**
     * 黑名单list
     * @return
     */
    @Override
    @Transactional
    public List idCard() {
        QApExpertCreditEntity qExpert = QApExpertCreditEntity.apExpertCreditEntity;
        Predicate predicate = qExpert.isNotNull().or(qExpert.isNull());
        predicate = ExpressionUtils.and(predicate, qExpert.delFlag.eq(Constant.NO_DELETE)) ;
        predicate = ExpressionUtils.and(predicate,qExpert.excpresState.eq(Constant.EXCPRES_STATE_Yes));

        List<ApExpertCreditEntity> list = jpaQueryFactory
                .selectFrom(qExpert)
                .where(
                        predicate
                )
                .fetch();
        List idCardList = new ArrayList();

        for(int i=0;i<list.size();i++){

          idCardList.add(list.get(i).getExpertIdcard());
        }
        return idCardList;
    }

    /**
     * 黑白名单单list
     * @return
     */
    @Override
    @Transactional
    public List idHbCard() {
        QApExpertCreditEntity qExpert = QApExpertCreditEntity.apExpertCreditEntity;
        Predicate predicate = qExpert.isNotNull().or(qExpert.isNull());
        predicate = ExpressionUtils.and(predicate, qExpert.delFlag.eq(Constant.NO_DELETE)) ;

        List<ApExpertCreditEntity> list = jpaQueryFactory
                .selectFrom(qExpert)
                .where(
                        predicate
                )
                .fetch();
        List idHbCardList = new ArrayList();

        for(int i=0;i<list.size();i++){

            idHbCardList.add(list.get(i).getExpertIdcard());
        }
        return idHbCardList;
    }


}
